package ece.com.model;

import java.util.Observable;
import java.util.Timer;
import java.util.TimerTask;

import ece.com.controler.LandingDoorControler;
import ece.com.gui.Gui_Landing;
import ece.com.intefaces.ILandingGear;


public class TimeLanding extends Observable implements ILandingGear{
	private String action="";
	private Gear g=null;
	private Door d=null;
	int timeVal=0; // en millisecondes

	public TimeLanding(String action,LandingGear lg, Gui_Landing guiGear){
		this.action=action;
		this.g=lg.getG();
		this.d=lg.getD();
		initTimeVal();
		executeTimer();
		LandingDoorControler ldC = new LandingDoorControler(lg, guiGear);
		this.addObserver(ldC);
	}
	
	public void initTimeVal(){
		
		//GEAR
		if(action.equals(ACTION_GEAR_UP_TO_DOWN)){
			if(g.getName().equals(FRONT))
				timeVal=1200;
			else if(g.getName().equals(LEFT))
				timeVal=1600;
			else if(g.getName().equals(RIGHT))
				timeVal=1600;
		}
		else if (action.equals(ACTION_GEAR_DOWN_TO_UP)){
			if(g.getName().equals(FRONT))
				timeVal=1600;
			else if(g.getName().equals(LEFT))
				timeVal=2000;
			else if(g.getName().equals(RIGHT))
				timeVal=2000;
		}
		else if (action.equals(ACTION_GEAR_UNLOCK_DOWN)){
			if(g.getName().equals(FRONT))
				timeVal=800;
			else if(g.getName().equals(LEFT))
				timeVal=800;
			else if(g.getName().equals(RIGHT))
				timeVal=800;
		}
		else if (action.equals(ACTION_GEAR_LOCK_DOWN)){
			if(g.getName().equals(FRONT))
				timeVal=400;
			else if(g.getName().equals(LEFT))
				timeVal=400;
			else if(g.getName().equals(RIGHT))
				timeVal=400;
		}
		else if (action.equals(ACTION_GEAR_UNLOCK_UP)){
			if(g.getName().equals(FRONT))
				timeVal=800;
			else if(g.getName().equals(LEFT))
				timeVal=800;
			else if(g.getName().equals(RIGHT))
				timeVal=800;
		}
		else if (action.equals(ACTION_GEAR_LOCK_UP)){
			if(g.getName().equals(FRONT))
				timeVal=400;
			else if(g.getName().equals(LEFT))
				timeVal=400;
			else if(g.getName().equals(RIGHT))
				timeVal=400;
		}
		
		//DOOR
		if(action.equals(ACTION_DOOR_DOWN_TO_UP)){
			if(d.getName().equals(FRONT))
				timeVal=1200;
			else if(d.getName().equals(RIGHT))
				timeVal=1600;
			else if(d.getName().equals(LEFT))
				timeVal=1600;
		}
		else if(action.equals(ACTION_DOOR_UP_TO_DOWN)){
			if(d.getName().equals(FRONT))
				timeVal=1200;
			else if(d.getName().equals(RIGHT))
				timeVal=1500;
			else if(d.getName().equals(LEFT))
				timeVal=1500;
		}
		else if(action.equals(ACTION_DOOR_LOCK_UP)){
			if(d.getName().equals(FRONT))
				timeVal=300;
			else if(d.getName().equals(RIGHT))
				timeVal=300;
			else if(d.getName().equals(LEFT))
				timeVal=300;
		}
		else if(action.equals(ACTION_DOOR_UNLOCK_UP)){
			if(d.getName().equals(FRONT))
				timeVal=400;
			else if(d.getName().equals(RIGHT))
				timeVal=400;
			else if(d.getName().equals(LEFT))
				timeVal=400;
		}
		
		//ajout des 20%
		double add = timeVal*(0.2);
		int add2 = (int) add;
		timeVal+=add2;
	}
	
	public void executeTimer(){
		System.out.println("TimeVal : "+timeVal);
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				
				try {
					
					Thread.sleep(timeVal);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				System.out.println(" DEBUT Time controler sleep "+action+" : " +timeVal +"ms");
				
				//GEAR
			    if(action.equals(ACTION_GEAR_UP_TO_DOWN) ){
			    	if(!g.isFromHightToDown()){
//			    		System.out.println("FAILED : "+action);
			    	setChanged();
					notifyObservers(ACTION_GEAR_UP_TO_DOWN);}
			    }
			    if(action.equals(ACTION_GEAR_DOWN_TO_UP) && !g.isFromDownToHight()){
//			    	System.out.println("FAILED : "+action);
			    	setChanged();
					notifyObservers(ACTION_GEAR_DOWN_TO_UP);
			    }
			    if(action.equals(ACTION_GEAR_UNLOCK_DOWN) && !g.isUnlockedDown()){
//			    	System.out.println("FAILED : "+action);
			    	setChanged();
					notifyObservers(ACTION_GEAR_UNLOCK_DOWN);
			    }
			    if(action.equals(ACTION_GEAR_LOCK_DOWN) ){
			    	if(!g.isLockedDown()){
//			    	System.out.println("FAILED : "+action);
			    	setChanged();
					notifyObservers(ACTION_GEAR_LOCK_DOWN);
			    	}
			    }
			    if(action.equals(ACTION_GEAR_UNLOCK_UP) && !g.isUnlockedUp()){
//			    	System.out.println("FAILED : "+action);
			    	setChanged();
					notifyObservers(ACTION_GEAR_UNLOCK_UP);
			    }
			    if(action.equals(ACTION_GEAR_LOCK_UP) && !g.isLockedUp()){
//			    	System.out.println("FAILED : "+action);
			    	setChanged();
					notifyObservers(ACTION_GEAR_LOCK_UP);
			    }
			    //DOOR
			    if(action.equals(ACTION_DOOR_UP_TO_DOWN) && !d.isFromHightToDown()){
//			    	System.out.println("FAILED : "+action);
			    	setChanged();
					notifyObservers(ACTION_DOOR_UP_TO_DOWN);
			    }
			    if(action.equals(ACTION_DOOR_DOWN_TO_UP) && !d.isFromDownToHight()){
//			    	System.out.println("FAILED : "+action);
			    	setChanged();
					notifyObservers(ACTION_DOOR_DOWN_TO_UP);
			    }
			    if(action.equals(ACTION_DOOR_UNLOCK_UP) && !d.isUnlock()){
//			    	System.out.println("FAILED : "+action);
			    	setChanged();
					notifyObservers(ACTION_DOOR_UNLOCK_UP);
			    }
			    if(action.equals(ACTION_DOOR_LOCK_UP) && !d.isLock()){
//			    	System.out.println("FAILED : "+action);
			    	setChanged();
					notifyObservers(ACTION_DOOR_LOCK_UP);
			    }
//			    System.out.println(" FIN Time controler sleep "+action+" : " +timeVal +"ms");
			    
			}
		}).start();
		
//		Timer time = new Timer();
//		time.schedule(new TimerTask() {
//			  @Override
//			  public void run() {
//				
////			    if(action.equals(ACTION_GEAR_UNLOCK_DOWN))
//			    
//			  }
//			}, timeVal);
	}
	
	public Gear getG() {
		return g;
	}

	public void setG(Gear g) {
		this.g = g;
	}
}
